package com.hckj.jianzhijia;

import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.avos.avoscloud.AVException;
import com.avos.avoscloud.AVObject;
import com.avos.avoscloud.AVQuery;
import com.hckj.jianzhijia.controllers.JobDetailsController;
import com.hckj.jianzhijia.listeners.OnBackClickListener;
import com.hckj.jianzhijia.model.Identification;
import com.hckj.jianzhijia.model.Job;
import com.hckj.jianzhijia.utils.DateUtils;
import com.hckj.jianzhijia.utils.ExceptionUtils;
import com.hckj.jianzhijia.utils.ToastUtils;

import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;

public class JobDetailsActivity extends BaseActivity {

    @InjectView(R.id.imgv_go_back)
    ImageView imgvGoBack;
    @InjectView(R.id.rl_toolbar)
    RelativeLayout rlToolbar;
    @InjectView(R.id.tv_user_name)
    TextView tvJobName;
    @InjectView(R.id.tv_publish_time)
    TextView tvPublishTime;
    @InjectView(R.id.tv_job_state)
    TextView tvJobState;
    @InjectView(R.id.tv_job_type)
    TextView tvJobType;
    @InjectView(R.id.tv_job_date)
    TextView tvJobDate;
    @InjectView(R.id.tv_job_time)
    TextView tvJobTime;
    @InjectView(R.id.tv_job_region)
    TextView tvJobRegion;
    @InjectView(R.id.tv_job_address)
    TextView tvJobAddress;
    @InjectView(R.id.tv_job_people_request)
    TextView tvJobPeopleRequest;
    @InjectView(R.id.tv_job_people_salary)
    TextView tvJobPeopleSalary;
    @InjectView(R.id.tv_job_gender_request)
    TextView tvJobGenderRequest;
    @InjectView(R.id.tv_job_describe)
    TextView tvJobDescribe;
    @InjectView(R.id.tv_job_contact_phone)
    TextView tvJobContactPhone;
    @InjectView(R.id.tv_job_contact_email)
    TextView tvJobContactEmail;
    @InjectView(R.id.tv_job_contact_people)
    TextView tvJobContactPeople;


    private Job job;
    private JobDetailsController jobDetailsController;
    private ProgressDialog progressDialog;

    private String nationName = "";
    private String regionName = "";
    private String cityName = "";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_job_details);
        ButterKnife.inject(this);
        jobDetailsController = new JobDetailsController(this);
        AVObject avObject =  (AVObject) getIntent().getParcelableExtra("JobAVObject");
        job = new Job(avObject);

        progressDialog = new ProgressDialog(this);
        progressDialog.setMessage("正在加载...");
        progressDialog.setCancelable(false);
        JobDetailTask getIdentificationInfoTask = new JobDetailTask();
        getIdentificationInfoTask.execute();


    }

    private void initEvent() {
        imgvGoBack.setOnClickListener(new OnBackClickListener(this));

    }

    private void initData() {

        tvJobName.setText(job.getJobName());
        tvPublishTime.setText(DateUtils.getDateStringFromDate(job.getCreatedAt()));
        tvJobState.setText(jobDetailsController.getJobStateString(job.getJobState()));
        tvJobType.setText(jobDetailsController.getStringByJobType(job.getJobType()));
        tvJobDate.setText(job.getWorkDate());
        tvJobTime.setText(job.getWorkTime());
        tvJobRegion.setText("" + nationName + regionName + cityName);
        tvJobAddress.setText(job.getWorkAddress());
        tvJobPeopleRequest.setText(job.getRequestNum() );
        tvJobDescribe.setText(job.getDescribe());
        tvJobContactPhone.setText(job.getContactPhone());
        tvJobContactEmail.setText(job.getContactEmail());
        tvJobContactPeople.setText(job.getContactName());

    }

    class JobDetailTask extends AsyncTask<Void, Void, String> {


        @Override
        protected String doInBackground(Void... params) {
            AVQuery avQuery = new AVQuery("province");
            avQuery.whereEqualTo("provinceId",job.getProvinceId());

            AVQuery avQuery2 = new AVQuery("cities");
            avQuery2.whereEqualTo("cityId",job.getCityId());

            AVQuery avQuery3 = new AVQuery("region");
            avQuery3.whereEqualTo("regionId",job.getRegionId());
            try {
                List list = avQuery.find();
                AVObject avObject = (AVObject) list.get(0);
                nationName = avObject.getString("province");

                List list2= avQuery2.find();
                AVObject avObject2 = (AVObject) list2.get(0);
                regionName = avObject2.getString("city");

                List list3 = avQuery3.find();
                AVObject avObject3 = (AVObject) list3.get(0);
                cityName = avObject3.getString("region");


            } catch (AVException e) {
                e.printStackTrace();
                return ExceptionUtils.getExceptionInfo(e);
            }
            return null;
        }

        @Override
        protected void onPreExecute() {
            progressDialog.show();
            super.onPreExecute();
        }

        @Override
        protected void onPostExecute(String s) {
            progressDialog.dismiss();
            initData();
            initEvent();
            super.onPostExecute(s);
        }
    }


}
